#include "VSSceneMap.h"
#include "VSGraphicInclude.h"
#include "VSStream.h"
#include "VSSceneManager.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSSceneMap,VSObject)
BEGIN_ADD_PROPERTY(VSSceneMap,VSObject)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSSceneMap)

IMPLEMENT_INITIAL_END
VSSceneMap::VSSceneMap(const TCHAR * pName)
{
	m_Name = pName;
	m_pScene = VS_NEW VSScene();
	VSSceneManager::ms_pSceneManager->AddScene(m_pScene);
}
VSSceneMap::~VSSceneMap()
{
	VSSceneManager::ms_pSceneManager->DeleteScene(m_pScene);
	m_pScene = NULL;
}
VSSceneMap::VSSceneMap()
{
	m_pScene = VS_NEW VSScene();
	VSSceneManager::ms_pSceneManager->AddScene(m_pScene);
}
void VSSceneMap::AddActor(VSActor * pActor)
{

	if (pActor)
	{
		for (unsigned int i = 0 ; i < m_pActorArray.GetNum() ; i++)
		{
			if (pActor == m_pActorArray[i])
			{
				pActor->m_pSceneMap = this;
				return ;
			}
		}
		if (pActor->m_pSceneMap)
		{
			pActor->m_pSceneMap->DeleteActor(pActor);
		}
		
		m_pActorArray.AddElement(pActor);
		if (pActor->GetActorType() == VSActor::AT_STATIC 
			|| pActor->GetActorType() == VSActor::AT_SKELECTON
			|| pActor->GetActorType() == VSActor::AT_TERRAIN)
		{
			m_pScene->AddObject((VSSpatial*)pActor->GetActorNode());
		}
		else if (pActor->GetActorType() == VSActor::AT_LIGHT)
		{
			m_pScene->AddLight((VSLight*)pActor->GetActorNode());
		}
		else if (pActor->GetActorType() == VSActor::AT_CAMERA)
		{
			m_pScene->AddCamera((VSCamera*)pActor->GetActorNode());
		}
		pActor->m_pSceneMap = this;
	}
}
VSActor *VSSceneMap::GetActor(VSUsedName Name)
{
	for (unsigned int i = 0 ; i < m_pActorArray.GetNum() ; i++)
	{
		if (m_pActorArray[i]->m_ActorName == Name)
		{
			
			return m_pActorArray[i];
		}
	}	
	return NULL;
}

void VSSceneMap::DeleteActor(VSActor * pActor)
{

	for (unsigned int i = 0 ; i < m_pActorArray.GetNum() ; i++)
	{
		if (m_pActorArray[i] == pActor)
		{
			if (m_pActorArray[i]->GetActorType() == VSActor::AT_STATIC 
				|| m_pActorArray[i]->GetActorType() == VSActor::AT_SKELECTON
				|| pActor->GetActorType() == VSActor::AT_TERRAIN)
			{
				m_pScene->DeleteObject((VSSpatial *)m_pActorArray[i]->GetActorNode());
			}
			else if (pActor->GetActorType() == VSActor::AT_LIGHT)
			{
				m_pScene->DeleteLight((VSLight*)pActor->GetActorNode());
			}
			else if (pActor->GetActorType() == VSActor::AT_CAMERA)
			{
				m_pScene->DeleteCamera((VSCamera*)pActor->GetActorNode());
			}
			m_pActorArray[i]->m_pSceneMap = NULL;
			m_pActorArray.Erase(i);
			return ;
		}
	}
}